package com.slfx.qrcode.rest;

import com.slfx.qrcode.util.JasperReportUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
 * @fileName: JasperController
 * @author: zengingfa
 * @date: 2022/10/25 19:34
 * @description:
 */
@RestController
@Slf4j
public class JasperController {

    @GetMapping("/jasper")
    public void getReport(@RequestParam("type") String reportType, HttpServletResponse response)
            throws Exception {
        HashMap<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("name", "xiao ming");

        List<HashMap> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            HashMap<String, String> item = new HashMap();
            item.put("Field_1", "filed-" + i);
            item.put("Field_2", "filed-" + i);
            list.add(item);
        }
        String jasperPath = JasperReportUtil.getJasperFileDir("test1");
        if (reportType.equals("pdf")) {
            JasperReportUtil.exportToPdf(jasperPath, parameters, list, response);
        } else if (reportType.equals("html")) {
            JasperReportUtil.exportToHtml(jasperPath, parameters, list, response);
        }
    }


    @GetMapping("/jasperAssetA4")
    public void jasperAssetA4(@RequestParam("type") String reportType, HttpServletResponse response)
            throws Exception {
        HashMap<String, Object> parameters = new HashMap<String, Object>();
        List<HashMap> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            HashMap<String, String> item = new HashMap();
            item.put("companyName", "广东神龙飞仙有限责任公司" + i);
            item.put("assetCode", "资产编码1资产编码1资产编码1资产编码1资产编码1资产编码1资产编码1资产编码1" + i);
            item.put("assetName", "资产名称资产编码1资产编码1资产编码1" + i);
            item.put("usingComName", "使用公司名称" + i);
            item.put("usingDeptName", "使用部门名称" + i);
            item.put("usingUseName", "使用人名称" + i);
            item.put("buyDate", "2022-10-10");
            item.put("qrCode", "aaaa" + i);
            list.add(item);
        }
        String jasperPath = JasperReportUtil.getJasperFileDir("Blank_A4_2");
        if (reportType.equals("pdf")) {
            JasperReportUtil.exportToPdf(jasperPath, parameters, list, response);
        } else if (reportType.equals("html")) {
            JasperReportUtil.exportToHtml(jasperPath, parameters, list, response);
        }
    }



    @GetMapping("/jasperAssetA8")
    public void jasperAssetA8(@RequestParam("type") String reportType, HttpServletResponse response)
            throws Exception {
        HashMap<String, Object> parameters = new HashMap<String, Object>();
        List<HashMap> paramlist = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            HashMap<String, String> item = new HashMap();
            item.put("companyName", "广东神龙飞仙有限责任公司" + i);
            item.put("assetCode", "资产编码1资产编码1资产编码1资产编码1资产编码1资产编码1资产编码1资产编码1" + i);
            item.put("assetName", "资产名称资产编码1资产编码1资产编码1" + i);
            item.put("usingComName", "使用公司名称" + i);
            item.put("usingDeptName", "使用部门名称" + i);
            item.put("usingUseName", "使用人名称" + i);
            item.put("buyDate", "2022-10-10");
            item.put("qrCode", "aaaa" + i);
            paramlist.add(item);
        }
        //分为两组 ，一组A1,另一组A2
        List<HashMap> list = new ArrayList<>();
        for (int i = 0; i < paramlist.size(); i=i+2) {
            HashMap<String,String> hashMap1 = paramlist.get(i);
            HashMap<String, String> item = new HashMap();
            item.put("companyName1", hashMap1.get("companyName"));
            item.put("assetCode1", hashMap1.get("assetCode"));
            item.put("assetName1", hashMap1.get("assetName"));
            item.put("usingComName1", hashMap1.get("usingComName"));
            item.put("usingDeptName1", hashMap1.get("usingDeptName"));
            item.put("usingUseName1", hashMap1.get("usingUseName"));
            item.put("buyDate1", hashMap1.get("buyDate"));
            item.put("qrCode1", hashMap1.get("qrCode"));
            HashMap<String,String> hashMap2 = paramlist.get(i+1);
            if(Objects.isNull(hashMap2)){
                item.put("companyName2", hashMap1.get("companyName"));
                item.put("assetCode2", hashMap1.get("assetCode"));
                item.put("assetName2", hashMap1.get("assetName"));
                item.put("usingComName2", hashMap1.get("usingComName"));
                item.put("usingDeptName2", hashMap1.get("usingDeptName"));
                item.put("usingUseName2", hashMap1.get("usingUseName"));
                item.put("buyDate2", hashMap1.get("buyDate"));
                item.put("qrCode2", hashMap1.get("qrCode"));
            }else {
                item.put("companyName2", hashMap2.get("companyName"));
                item.put("assetCode2", hashMap2.get("assetCode"));
                item.put("assetName2", hashMap2.get("assetName"));
                item.put("usingComName2", hashMap2.get("usingComName"));
                item.put("usingDeptName2", hashMap2.get("usingDeptName"));
                item.put("usingUseName2", hashMap2.get("usingUseName"));
                item.put("buyDate2", hashMap2.get("buyDate"));
                item.put("qrCode2", hashMap2.get("qrCode"));
            }
            list.add(item);
        }

        String jasperPath = JasperReportUtil.getJasperFileDir("Blank_A8_3");
        if (reportType.equals("pdf")) {
            JasperReportUtil.exportToPdf(jasperPath, parameters, list, response);
        } else if (reportType.equals("html")) {
            JasperReportUtil.exportToHtml(jasperPath, parameters, list, response);
        }
    }

}
